<template>
  <div>
    <HyForm v-bind="searchFormConfig" v-model="formDate">
      <template #header>
        <h1 class="header">高级检索</h1>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button :icon="Refresh" @click="handleResetClick">重置</el-button>
          <el-button type="primary" :icon="Search" @click="handleQueryClick">搜索</el-button>
        </div>
        <div style="clear: both"></div>
      </template>
    </HyForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import HyForm from '@/base-ui/form'
import { Refresh, Search } from '@element-plus/icons-vue'

export default defineComponent({
  name: 'pageSearch',
  props: {
    searchFormConfig: {
      type: Object,
      reuqired: true
    }
  },
  components: { HyForm },
  emits: ['resetBtnClick', 'queryBtnClick'],
  setup(props, { emit }) {
    // 双向绑定的属性应该是由配置文件的field来决定
    // 1. 优化1 formData中的属性应该动态来决定
    const formItems = props.searchFormConfig?.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ''
    }

    const formDate = ref(formOriginData)
    // 2 优化二 当我们用户点击了重置的时候
    const handleResetClick = () => {
      for (const key in formOriginData) {
        formDate.value[key] = formOriginData[key]
      }
      emit('resetBtnClick')
      // formDate.value = formOriginData
    }

    // 优化3：当用户点击搜索
    const handleQueryClick = () => {
      console.log(formDate.value)
      emit('queryBtnClick', formDate.value)
    }

    return { Refresh, formDate, Search, handleResetClick, handleQueryClick }
  }
})
</script>

<style scoped>
.handle-btns {
  float: right;
  margin: 0 20px 30px 0;
}
</style>
